package com.xkdx.guangguang.presistence.attention;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.shareclass.BrandDetail;
import com.xkdx.guangguang.shareclass.Shop;

public class GetAttentionModule extends AbsModule {
	public List<Shop> shopList;
	public List<BrandDetail> brandList;

	@Override
	public void parseData() throws Exception {
		backJson = (JSONArray) result;
		try {
			for (int i = 0; i < backJson.length(); i++) {
				jsonObj = (JSONObject) backJson.get(i);
				String sortOfAct = jsonObj.getString("Act");
				if (sortOfAct.equals("getAttentShopList")) {
					shopList = parseAttentShop(jsonObj.getJSONObject("Content"));
				} else if (sortOfAct.equals("getAttentBrandList")) {
					brandList = parseAttentBrand(jsonObj
							.getJSONObject("Content"));
				}

			}
		} catch (Exception e) {
			throw e;
		}

	}

	private List<Shop> parseAttentShop(JSONObject person) throws Exception {
		// TODO Auto-generated method stub
		List<Shop> shops = new ArrayList<Shop>();

		try {
			JSONObject stut = (JSONObject) person.get("Result");
			if (stut.has("ListInfo") && !stut.isNull("ListInfo")) {
				JSONArray list = stut.getJSONArray("ListInfo");
				for (int i = 0; i < list.length(); i++) {
					JSONObject obj = (JSONObject) list.opt(i);
					Shop shop = new Shop();
					shop.setShopID(obj.getString("ShopID"));
					shop.setLogo(obj.getString("Logo"));
					shop.setShopName(obj.getString("ShopName"));
					shop.setShopType(obj.getString("ShopType"));
					shops.add(shop);
				}
			}

		} catch (Exception e) {
			throw e;
		}
		return shops;
	}

	private List<BrandDetail> parseAttentBrand(JSONObject person)
			throws Exception {
		// TODO Auto-generated method stub
		List<BrandDetail> brands = new ArrayList<BrandDetail>();

		try {
			JSONObject stut = (JSONObject) person.get("Result");
			JSONArray list = stut.getJSONArray("ListInfo");
			for (int i = 0; i < list.length(); i++) {
				JSONObject obj = (JSONObject) list.opt(i);
				BrandDetail brand = new BrandDetail();
				brand.setBrandID(Integer.valueOf(obj.getString("BrandID")));
				brand.setLogo(obj.getString("Logo"));
				brand.setBrandName(obj.getString("BrandName"));
				brands.add(brand);
			}

		} catch (Exception e) {
			throw e;
		}
		return brands;
	}
}
